package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ea.demo.autogenapi.base.vo.DefaultResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import ${cfg.controllerVoPackage}.*;
import ${package.Entity}.*;
import ${package.Service}.*;


/**
* <p>
* ${table.comment!} 前端控制器
* </p>
*
* @author ${author}
* @since ${date}
*/
@Slf4j
@Api(tags = {"${table.comment!}"})
@RestController
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
    <#assign entityServiceVar="${entity?uncap_first}Service"/>
    <#assign apiWrapperClassName="${entity}ApiWrapper"/>
    <#assign entityVar="${entity?uncap_first}"/>
    <#assign entityVo="${entity}Vo"/>
    <#assign entityVoVar="${entityVo?uncap_first}"/>
    <#assign entitySearchVo="${entity}SearchVo"/>
    <#assign entitySearchVoVar="${entitySearchVo?uncap_first}"/>

    @Autowired
    private ${table.serviceName}  ${entity?uncap_first}Service;



    <#-- 新增接口 -->
    @PostMapping("/add")
    @ApiOperation("新增操作")
    public DefaultResponseVo<${apiWrapperClassName}.${entityVo}> add(@RequestBody ${apiWrapperClassName}.${entityVo} ${entityVoVar}){
        Assert.notNull(${entityVoVar},"参数非法.");
        ${entity} ${entityVar} = new ${entity}();
        BeanUtils.copyProperties(${entityVoVar},${entityVar});
        ${entityServiceVar}.save(frontUser);
        ${entityVoVar}.setId(${entityVar}.getId());
        return DefaultResponseVo.success("success",${entityVoVar});
    }

    <#-- 更新接口 -->
    @PostMapping("/update")
    @ApiOperation("修改操作")
    public DefaultResponseVo<${apiWrapperClassName}.${entityVo}> update(@RequestBody ${apiWrapperClassName}.${entityVo} ${entityVoVar}){
        Assert.notNull(${entityVoVar},"参数非法.");
        ${entity} ${entityVar} = new ${entity}();
        BeanUtils.copyProperties(${entityVoVar},${entityVar});
        ${entityServiceVar}.updateById(${entityVar});
        return DefaultResponseVo.success("success",${entityVoVar});
    }

    <#-- 单记录获取接口 -->
    @GetMapping("/get")
    @ApiOperation("获取单记录操作")
    public DefaultResponseVo<${apiWrapperClassName}.${entityVo}> get(@RequestParam Integer id){
        Assert.notNull(id,"参数非法.");
        ${entity} ${entityVar} = ${entityServiceVar}.getById(id);
        if(${entityVar}==null)
            return DefaultResponseVo.success("success",null);
        ${apiWrapperClassName}.${entityVo} ${entityVoVar} = new ${apiWrapperClassName}.${entityVo}();
        BeanUtils.copyProperties(${entityVar},${entityVoVar});
        return DefaultResponseVo.success("success",${entityVoVar});
    }
    <#-- 查询接口 -->
    @PostMapping("/query")
    @ApiOperation("查询操作")
    public DefaultResponseVo<Page<${apiWrapperClassName}.${entityVo}>> query(@RequestBody ${apiWrapperClassName}.${entitySearchVo} ${entitySearchVoVar}){
        Assert.notNull(${entitySearchVoVar},"参数非法.");
        Page<${entity}> page = new Page<>(${entitySearchVoVar}.getPageNo(),${entitySearchVoVar}.getPageSize());
        QueryWrapper<${entity}> wrapper = new QueryWrapper<>();
        //TODO:查询条件添加
        Page<${entity}> listPage = ${entityServiceVar}.page(page,wrapper);
        //TODO:结果对象转换
        return DefaultResponseVo.success("success",null);
    }


}
